import Vue from 'vue';
import Router from 'vue-router';
import store from '@/store';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

const NotFoundComponent = resolve => require(['@/components/NotFoundComponent'], resolve);
const Home = resolve => require(['@/views/Home'], resolve);
const Test = resolve => require(['@/views/Test'], resolve);
const Test2 = resolve => require(['@/views/Test/index2.vue'], resolve);
const Shortage = resolve => require(['@/views/Shortage'], resolve);
const Feedback = resolve => require(['@/views/Shortage/Feedback'], resolve);
const FeedbackList = resolve => require(['@/views/Shortage/Feedback/FeedbackList'], resolve);
const WuliaoF = resolve => require(['@/views/Shortage/Feedback/Wuliao'], resolve);
const HospitalF = resolve => require(['@/views/Shortage/Feedback/Hospital'], resolve);
const FavoritesList = resolve => require(['@/views/Shortage/Favorites/FavoritesList'], resolve);
const Inventory = resolve => require(['@/views/Inventory'], resolve);
const Inventorymx = resolve => require(['@/views/Inventory/Inventorymx'], resolve);
const Order = resolve => require(['@/views/Order'], resolve);
const OrderOrDetail = resolve => require(['@/views/Order/OrderOrDeail'], resolve);
const OrderList = resolve => require(['@/views/Order/OrderList'], resolve);
const OrderDetail = resolve => require(['@/views/Order/OrderList/OrderDetail'], resolve);
const OrderPicture = resolve => require(['@/views/Order/OrderList/OrderDetail/OrderPicture'], resolve);
const Profit = resolve => require(['@/views/Profit'], resolve);
const FeedbackP = resolve => require(['@/views/Profit/Feedback'], resolve);
const WuliaoP = resolve => require(['@/views/Profit/Feedback/Wuliao'], resolve);
const HospitalP = resolve => require(['@/views/Profit/Feedback/Hospital'], resolve);
const FeedbackpList = resolve => require(['@/views/Profit/Feedback/FeedbackList'], resolve);
const Unmarketable = resolve => require(['@/views/Unmarketable'], resolve);
const FeedbackU = resolve => require(['@/views/Unmarketable/Feedback'], resolve);
const WuliaoU = resolve => require(['@/views/Unmarketable/Feedback/Wuliao'], resolve);
const HospitalU = resolve => require(['@/views/Unmarketable/Feedback/Hospital'], resolve);
const FeedbackuList = resolve => require(['@/views/Unmarketable/Feedback/FeedbackList'], resolve);
const Svc = resolve => require(['@/views/Svc'], resolve);
const Svcstand = resolve => require(['@/views/Svc/Svcstand'], resolve);
const Project = resolve => require(['@/views/Project'], resolve);
const ProjectInfo = resolve => require(['@/views/Project/ProjectInfo'], resolve);
const Projectdetail = resolve => require(['@/views/Project/ProjectInfo/Projectdetail'], resolve);
const Forecast = resolve => require(['@/views/Forecast'], resolve);
const NewList = resolve => require(['@/views/Forecast/NewList'], resolve);
const HistoryList = resolve => require(['@/views/Forecast/HistoryList'], resolve);
const SaleSubList = resolve => require(['@/views/Forecast/SaleSubList'], resolve);
const SaleAdd = resolve => require(['@/views/Forecast/SaleSubList/saleAdd'], resolve);
const WuliaoM = resolve => require(['@/views/Forecast/SaleSubList/saleAdd/Wuliao'], resolve);
const HospitalM = resolve => require(['@/views/Forecast/SaleSubList/saleAdd/Hospital'], resolve);
const ProjectS = resolve => require(['@/views/ProjectS'], resolve);
const ProjectInfoS = resolve => require(['@/views/ProjectS/ProjectInfoS'], resolve);
const ProjectdetailS = resolve => require(['@/views/ProjectS/ProjectInfoS/ProjectdetailS'], resolve);
const ProjectImgS = resolve => require(['@/views/ProjectS/ProjectInfoS/ProjectdetailS/projectImgS'], resolve);
const Communicate = resolve => require(['@/views/Communicate'], resolve);
const CommunicateAdd = resolve => require(['@/views/Communicate/add'], resolve);
const CommunicateMember = resolve => require(['@/views/Communicate/member'], resolve);

Vue.use(Router)

const router = new Router({
  // history 模式打包后不显示
  mode: 'history',
  base: '/cx/',
  routes: [
    {
      path: '/',
      name: 'Home',
      component: Home,
      meta: {title: "首页"},
      children: [
        {
          path: '/test',
          name: 'Test',
          component: Test,
          meta: {title: "测试"}
        },
        {
          path: '/test2',
          name: 'Test2',
          component: Test2,
          meta: {title: "测试2"}
        },
        {
          path: '/shortage',
          name: 'Shortage',
          component: Shortage,
          meta: {title: "缺货信息"},
          children: [
            {
              path: 'feedback',
              name: 'Feedback',
              component: Feedback,
              meta: {title: "缺货反馈"},
              children: [
                {
                  path: 'wuliaof',
                  name: 'WuliaoF',
                  component: WuliaoF,
                  meta: {title: "物料搜索"}
                },
                {
                  path: 'hospitalf',
                  name: 'HospitalF',
                  component: HospitalF,
                  meta: {title: "医院搜索"}
                },
                {
                  path: 'feedbacklist',
                  name: 'FeedbackList',
                  component: FeedbackList,
                  meta: {title: "缺货反馈历史"}
                }
              ]
            },
            {
              path: 'favoritesList',
              name: 'FavoritesList',
              component: FavoritesList,
              meta: {title: "收藏夹"},
            }
          ]
        },
        {
          path: '/inventory',
          name: 'Inventory',
          component: Inventory,
          meta: {title: "库存"},
          children: [
            {
              path: 'inventorymx/:id',
              name: 'Inventorymx',
              component: Inventorymx,
              meta: {title: "库存价格"},
              children: []
            }
          ]
        },
        {
          path: '/order',
          name: 'Order',
          component: Order,
          meta: {title: "订单"},
          children: [
            {
              path: 'orderList/:id',
              name: 'OrderList',
              component: OrderList,
              meta: {title: "订单列表"},
              children: [
                {
                  path: 'orderDetail/:sid',
                  name: 'OrderDetail',
                  component: OrderDetail,
                  meta: {title: "订单详情"},
                  children: [
                    {
                      path: 'orderPicture',
                      name: 'OrderPicture',
                      component: OrderPicture,
                      meta: {title: "包装盒"},
                    }
                  ]
                }
              ]
            },
            {
              path: '/order/orderordeail',
              name: 'OrderOrDetail',
              component: OrderOrDetail,
              meta: {title: "订单品种列表"},
              children: []
            }
          ]
        },
        {
          path: '/profit',
          name: 'Profit',
          component: Profit,
          meta: {title: "高毛利品种"},
          children: [
            {
              path: 'feedbackp',
              name: 'FeedbackP',
              component: FeedbackP,
              meta: {title: "高毛利回复"},
              children: [
                {
                  path: 'wuliaop',
                  name: 'WuliaoP',
                  component: WuliaoP,
                  meta: {title: "物料搜索"}
                },
                {
                  path: 'hospitalp',
                  name: 'HospitalP',
                  component: HospitalP,
                  meta: {title: "医院搜索"}
                },
                {
                  path: 'feedbackplist',
                  name: 'FeedbackpList',
                  component: FeedbackpList,
                  meta: {title: "高毛利回复历史"}
                }
              ]
            }
          ]
        },
        {
          path: '/unmarketable',
          name: 'Unmarketable',
          component: Unmarketable,
          meta: {title: "滞销品种"},
          children: [
            {
              path: 'feedbacku',
              name: 'FeedbackU',
              component: FeedbackU,
              meta: {title: "滞销回复"},
              children: [
                {
                  path: 'wuliaou',
                  name: 'WuliaoU',
                  component: WuliaoU,
                  meta: {title: "物料搜索"}
                },
                {
                  path: 'hospitalu',
                  name: 'HospitalU',
                  component: HospitalU,
                  meta: {title: "医院搜索"}
                },
                {
                  path: 'feedbackulist',
                  name: 'FeedbackuList',
                  component: FeedbackuList,
                  meta: {title: "滞销回复历史"}
                }
              ]
            }
          ]
        },
        {
          path: '/svc',
          name: 'Svc',
          component: Svc,
          meta: {title: "SVC查询"},
          children: [
            {
              path: 'svcstand',
              name: 'Svcstand',
              component: Svcstand,
              meta: {title: "SVC推荐表"}
            }
          ]
        },
        {
          path: '/project',
          name: 'Project',
          component: Project,
          meta: {title: "采购项目"},
          children: [
            {
              path: 'projectInfo',
              name: 'ProjectInfo',
              component: ProjectInfo,
              meta: {title: "采购项目"},
              children: [
                {
                  path: 'projectdetail',
                  name: 'Projectdetail',
                  component: Projectdetail,
                  meta: {title: "项目明细"}
                }
              ]
            }
          ]
        },
        {
          path: '/projects',
          name: 'ProjectS',
          component: ProjectS,
          meta: {title: "采购项目"},
          children: [
            {
              path: 'projectInfoS',
              name: 'ProjectInfoS',
              component: ProjectInfoS,
              meta: {title: "采购项目"},
              children: [
                {
                  path: 'projectdetailS',
                  name: 'ProjectdetailS',
                  component: ProjectdetailS,
                  meta: {title: "项目明细"},
                  children: [
                    {
                      path: 'projectImgS',
                      name: 'ProjectImgS',
                      component: ProjectImgS,
                      meta: {title: "上传证据"},
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          path: '/forecast',
          name: 'Forecast',
          component: Forecast,
          meta: {title: "大石头客户预测"},
          children: [
            {
              path: 'newList',
              name: 'NewList',
              component: NewList,
              meta: {title: "待反馈信息"}
            },
            {
              path: 'historyList',
              name: 'HistoryList',
              component: HistoryList,
              meta: {title: "历史预测查询"}
            },
            {
              path: 'saleSubList',
              name: 'SaleSubList',
              component: SaleSubList,
              meta: {title: "销售主动上报"},
              children: [
                {
                  path: 'saleAdd',
                  name: 'SaleAdd',
                  component: SaleAdd,
                  meta: {title: "上报"},
                  children: [
                    {
                      path: 'wuliaom',
                      name: 'WuliaoM',
                      component: WuliaoM,
                      meta: {title: "物料搜索"}
                    },
                    {
                      path: 'hospitalm',
                      name: 'HospitalM',
                      component: HospitalM,
                      meta: {title: "医院搜索"}
                    }
                  ]
                }
              ]
            }
          ]
        },
        {
          path: 'communicate',
          name: 'Communicate',
          component: Communicate,
          meta: {title: "业务员沟通"},
          children: [
            {
              path: 'add',
              name: 'CommunicateAdd',
              component: CommunicateAdd,
              meta: {title: "创建沟通"},
              children: [
                {
                  path: 'member',
                  name: 'CommunicateMember',
                  component: CommunicateMember,
                  meta: {title: "选择人员"},
                }
              ]
            }
          ]
        }
			]
    },
    {
      path:'*',
      component: NotFoundComponent//自定义的404页面组件
    }
  ]
})

router.beforeEach(async (to, from, next) => {
  NProgress.start();
  /* const hasLogin = store.state.hasLogin;
  // 这里获取 permissionList
  await store.dispatch('getPermissionList')
  const loginUserInfo = store.state.loginUserInfo;
  console.log(JSON.stringify(loginUserInfo)) */
  if (to.meta.Login) {
    if(!hasLogin){
      // next({path: '/login'});
      next();
      NProgress.done();
    }else{
      next();
      NProgress.done();
    }
  } else {
    next();
    NProgress.done();
  }
});

router.afterEach((to, from) => {
  if(to.meta.title){
    document.title = to.meta.title
  }
  NProgress.done();
})

export default router;
